package com.example.demo.Service;

import com.example.demo.Entity.CurrentStorage;
import com.example.demo.Entity.Item;
import com.example.demo.Entity.OperationLog;
import com.example.demo.Entity.Shelf;
import com.example.demo.Repository.CurrentStorageRepo;
import com.example.demo.Service.CurrentStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class CurrentStorageServiceImpl implements CurrentStorageService {

    private final CurrentStorageRepo currentStorageRepo;

    @Autowired
    public CurrentStorageServiceImpl(CurrentStorageRepo currentStorageRepo) {
        this.currentStorageRepo = currentStorageRepo;
    }

    @Override
    public CurrentStorage saveCurrentStorage(CurrentStorage currentStorage) {
        return currentStorageRepo.save(currentStorage);
    }

    @Override
    public Optional<CurrentStorage> getCurrentStorageById(Long id) {
        return currentStorageRepo.findById(id);
    }

    @Override
    public List<CurrentStorage> getAllCurrentStorage() {
        return currentStorageRepo.findAll();
    }

    @Override
    public void deleteCurrentStorageById(Long id) {
        currentStorageRepo.deleteById(id);
    }

    // 根据warehouseId和itemId获取CurrentStorage
    public Optional<CurrentStorage> findCurrentStorageByWarehouseIdAndItemId(Long warehouseId, Long itemId) {
        return currentStorageRepo.findByWarehouseIdAndItemId(warehouseId, itemId);
    }

    @Override
    public List<CurrentStorage> findByItem(Item item) {
        return currentStorageRepo.findByItem(item);
    }

    @Override
    public double currentSum(Item item) {
        List<CurrentStorage> storages = currentStorageRepo.findByItem(item);
        double sum = 0;
        for (CurrentStorage storage : storages) {
            sum +=  storage.getCurrentQuantity();
        }
        return sum;
    }
}